package com.cloud.logger.zchain.zchainclient.util;

import com.alibaba.fastjson.JSONObject;
import com.cloud.logger.zchain.core.common.entity.AngelaLoggerEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * User: wcy
 * Date: 2017/11/8
 * Time: 13:54
 */
public class TestKafka {

    private static Gson gson = new GsonBuilder().create();

   static String content = "{\n" +
           "      \"uuid\": \"cb697a741d514cd49d513e84137a47e2\",\n" +
           "      \"interfaceName\": \"/gis//ucTenant/getRightOther.json\",\n" +
           "      \"interfaceIp\": \"http://localhost:3201/gis//ucTenant/getRightOther.json\",\n" +
           "      \"interfacePort\": null,\n" +
           "      \"createTime\": null,\n" +
           "      \"updateTime\": null,\n" +
           "      \"elapsedTime\": 5,\n" +
           "      \"sourceIP\": \"127.0.0.1\",\n" +
           "      \"tenentId\": null,\n" +
           "      \"parms\": \"\",\n" +
           "      \"result\": \"\",\n" +
           "      \"type\": null,\n" +
           "      \"pattern\": \"INTERCEPTOR\",\n" +
           "      \"status\": null,\n" +
           "      \"alarmLevel\": null,\n" +
           "      \"parentNode\": null,\n" +
           "      \"method\": \"OPTIONS\",\n" +
           "      \"scenic\": \"drore\",\n" +
           "      \"project\": \"drore_gis\",\n" +
           "      \"description\": null\n" +
           "    }";

    public static void main(String[] args) {
//        a();
        work(1);
//        CompletableFuture.runAsync(()->b());
    }

    private static void a(){
        final int a = 1;
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
//线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(50000);
//线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(5);
//线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(300);
//线程池维护线程所允许的空闲时间
        poolTaskExecutor.setKeepAliveSeconds(30000);
        poolTaskExecutor.initialize();
        List<CompletableFuture> futures = new ArrayList<>();
        for(int i = 0; i < 50000; i++) {
            int finalI = i;
//            futures.add(CompletableFuture.runAsync(() -> work(finalI),poolTaskExecutor));
            work(finalI);
        }

        for(CompletableFuture future:futures){
            try {
                future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        System.out.println("结束");
    }

    private static void b(){
        final int a = 1;
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
//线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(50000);
//线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(5);
//线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(300);
//线程池维护线程所允许的空闲时间
        poolTaskExecutor.setKeepAliveSeconds(30000);
        poolTaskExecutor.initialize();
        List<CompletableFuture> futures = new ArrayList<>();
        for(int i = 0; i < 50000; i++) {
            int finalI = i;
            futures.add(CompletableFuture.runAsync(() -> work(finalI),poolTaskExecutor));
        }

//        for(CompletableFuture future:futures){
//            try {
//                future.get();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (ExecutionException e) {
//                e.printStackTrace();
//            }
//        }
        System.out.println("结束");
    }

    private static void work(int a2) {

        AngelaLoggerEntity logger = JSONObject.parseObject(content, AngelaLoggerEntity.class);
        KafkaUtil.saveLogger(JSONObject.parseObject(content, AngelaLoggerEntity.class));
        System.out.println(a2);
    }

}
